﻿using Sidvall.Serialization;
using System;
using System.Globalization;

namespace Sidvall.UI.Uwp.Converters
{
    public class DateTimeToStringConverter : ValueToStringConverterBase
    {
        #region Public Members

        #region StringToValue

        protected override object StringToValue(object value, Type targetType, object parameter, string language)
        {
            var culture = new CultureInfo(language);
            return value.ToNullableDateTime(culture, DateTimeStyles.None);
        }

        #endregion
        #region ValueToString

        protected override string ValueToString(object value, Type targetType, object parameter, string language)
        {
            var dateTime = (DateTime?)value;
            if (dateTime == null)
                return null;
            var culture = new CultureInfo(language);
            var format = parameter.ToString(null);
            if (!string.IsNullOrWhiteSpace(format))
                return dateTime.Value.ToString(format, culture);
            return dateTime.Value.ToString(culture);
        }

        #endregion

        #endregion
    }
}
